package civicinity.ist.psu.edu.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import civicinity.ist.psu.edu.R;
import civicinity.ist.psu.edu.adapter.CivicinityLazyAdapter.ViewHolder;
import civicinity.ist.psu.edu.constants.Constants;
import civicinity.ist.psu.edu.rssfeed.RSSDBItem;
import civicinity.ist.psu.edu.rssfeed.RSSFeed;
import civicinity.ist.psu.edu.rssfeed.RSSItem;

public class CivicinityRSSInfoAdapter extends BaseAdapter{
	private Context mContext;
	private LayoutInflater mInflater;
	private ArrayList<RSSDBItem> mArr;
	private int mLayout;
	public CivicinityImageLoader mImageLoader;
	
	public CivicinityRSSInfoAdapter(Context context, int layout, ArrayList<RSSDBItem> arr) {
		mContext = context;
		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mLayout = layout;
		mArr = arr;
		mImageLoader = new CivicinityImageLoader(context.getApplicationContext());
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mArr.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mArr.get(position).getRSSName();
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	public static class ViewHolder{
        public TextView rssTitle;
        public TextView rssUrl;
        public ImageView rssImage;
        public View thumbnail;
    }

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View vi=convertView;
        ViewHolder holder;
        if(convertView == null){
            vi = mInflater.inflate(R.layout.rss_item, null);
            holder = new ViewHolder();
            holder.rssTitle = (TextView)vi.findViewById(R.id.rss_title);
            holder.rssUrl = (TextView)vi.findViewById(R.id.rss_feed_date);
            holder.rssImage = (ImageView)vi.findViewById(R.id.rss_image);
            vi.setTag(holder);
        }
        else
            holder = (ViewHolder)vi.getTag();
        
        // for title & url
        holder.rssTitle.setText(mArr.get(position).getRSSName());
        holder.rssUrl.setText(mArr.get(position).getRSSUrl());
        // set current url invisible.
        holder.rssUrl.setVisibility(View.GONE);
        
        // for image
        holder.rssImage.setTag(Constants.RSS_ICON_URL + mArr.get(position).getRSSUrl());
        mImageLoader.DisplayImage(Constants.RSS_ICON_URL + mArr.get(position).getRSSUrl(), mContext, holder.rssImage);
        
        return vi;
	}
}